package com.ysyx.ysyx_passengerconfirmo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysyx.ysyx_passengerconfirmo.dto.BaseDto;
import com.ysyx.ysyx_passengerconfirmo.mapper.OrderdetailPojoMapper;
import com.ysyx.ysyx_passengerconfirmo.mapper.PassengerPojoMapper;
import com.ysyx.ysyx_passengerconfirmo.pojo.AreaPojo;
import com.ysyx.ysyx_passengerconfirmo.pojo.OrderPojo;
import com.ysyx.ysyx_passengerconfirmo.pojo.OrderdetailPojo;
import com.ysyx.ysyx_passengerconfirmo.pojo.PassengerPojo;
import com.ysyx.ysyx_passengerconfirmo.qo.TakeOrderQo;
import com.ysyx.ysyx_passengerconfirmo.service.OrderPojoService;
import com.ysyx.ysyx_passengerconfirmo.mapper.OrderPojoMapper;
import com.ysyx.ysyx_passengerconfirmo.service.PassengerPojoService;
import com.ysyx.ysyx_passengerconfirmo.utils.CostCalcUtils;
import com.ysyx.ysyx_passengerconfirmo.utils.MapToBeanUtils;
import org.redisson.api.GeoEntry;
import org.redisson.api.RGeo;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class OrderPojoServiceImpl extends ServiceImpl<OrderPojoMapper, OrderPojo>
    implements OrderPojoService{


    @Autowired
    private OrderPojoMapper orderPojoMapper;

    @Autowired
    private OrderdetailPojoMapper orderdetailPojoMapper;


    @Autowired
    private PassengerPojoMapper passengerPojoMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public BaseDto insertOneOrder(TakeOrderQo qo) {

        //获取订单终点区域
        RMap<Object, Object> fuzhouMap = redissonClient.getMap("area:Fuzhou");

//        TakeOrderQo qo = new TakeOrderQo(
//                1,
//                119.292198,26.062796,"A",
//                119.296198,26.066796,"B",
//                10.01,null);

//        GeoQo qo = new GeoQo(119.292198,26.062796,0);
        String startName = qo.getStartName();
        String endName = qo.getEndName();

        Double leftupLong = Double.parseDouble((String) fuzhouMap.get("leftupLongitude")) ;
        Double leftupLat = Double.parseDouble((String) fuzhouMap.get("leftupLatitude")) ;
        Double rightdownLong = Double.parseDouble((String) fuzhouMap.get("rightdownLongitude")) ;
        Double rightdownLat = Double.parseDouble((String) fuzhouMap.get("rightdownLatitude")) ;
        if (qo.getEndLongitude()>=leftupLong &&   //司机位置经度 >= 区域左上经度
                qo.getEndLongitude()<=rightdownLong &&  //司机位置经度 <= 区域右下经度
                qo.getEndLatitude()>=rightdownLat &&// 司机位置纬度 >= 区域右下纬度
                qo.getEndLatitude()<=leftupLat// 司机位置纬度 >= 区域左上纬度
        ){
            System.out.println("当前区域已开通服务");
            String areaName;
            List<RMap<Object,Object>> maps = new ArrayList<>();
            RMap<Object, Object> mapA1 = redissonClient.getMap("area:A1");
            RMap<Object, Object> mapA2 = redissonClient.getMap("area:A2");
            RMap<Object, Object> mapA3 = redissonClient.getMap("area:A3");
            RMap<Object, Object> mapB1 = redissonClient.getMap("area:B1");
            RMap<Object, Object> mapB2 = redissonClient.getMap("area:B2");
            RMap<Object, Object> mapB3 = redissonClient.getMap("area:B3");
            maps.add(mapA1);
            maps.add(mapA2);
            maps.add(mapA3);
            maps.add(mapB1);
            maps.add(mapB2);
            maps.add(mapB3);

            for (RMap<Object, Object> map : maps) {

                Double leftupLongitude = Double.parseDouble((String) map.get("leftupLongitude")) ;
                Double leftupLatitude = Double.parseDouble((String) map.get("leftupLatitude")) ;
                Double rightdownLongitude = Double.parseDouble((String) map.get("rightdownLongitude")) ;
                Double rightdownLatitude = Double.parseDouble((String) map.get("rightdownLatitude")) ;
                if (qo.getStartLongitude()>=leftupLongitude &&   //司机位置经度 >= 区域左上经度
                        qo.getStartLongitude()<=rightdownLongitude &&  //司机位置经度 <= 区域右下经度
                        qo.getStartLatitude()>=rightdownLatitude &&// 司机位置纬度 >= 区域右下纬度
                        qo.getStartLatitude()<=leftupLatitude// 司机位置纬度 >= 区域左上纬度
                )
                {
                    areaName =  map.getName();
                    System.out.println("当前订单位于 区块："+areaName);
                    System.out.println("生成订单中");

                    //生成订单插入数据库
                    //生成订单Id
                    String orderId = String.valueOf(IdWorker.getId());
                    qo.setOrderId(orderId);


                    qo.setOrderCost(CostCalcUtils.costCalc(qo.getOrderMileage()));

                    //数据注入
                    OrderPojo insertOrder = new OrderPojo();
                    insertOrder.setOrderId(orderId);
                    insertOrder.setOrderMileage(qo.getOrderMileage());
                    insertOrder.setStartName(qo.getStartName());
                    insertOrder.setEndName(qo.getEndName());
                    insertOrder.setPassengerId(qo.getPassengerId());
                    insertOrder.setOrderCost(qo.getOrderCost());
                    insertOrder.setOrderStatus(101);
                    int result = orderPojoMapper.insert(insertOrder);



                    if(result==1){

                        QueryWrapper<PassengerPojo> passengerQw = new QueryWrapper<>();
                        passengerQw.eq("passenger_id",qo.getPassengerId());
                        PassengerPojo passengerPojo = passengerPojoMapper.selectOne(passengerQw);



                        OrderdetailPojo insertDetail = new OrderdetailPojo();
                        insertDetail.setOrderId(orderId);
                        insertDetail.setStartLongitude(qo.getStartLongitude());
                        insertDetail.setStartLatitude(qo.getStartLatitude());

                        insertDetail.setStartLongitude(qo.getStartLongitude());
                        insertDetail.setEndLatitude(qo.getEndLatitude());
                        insertDetail.setEndLongitude(qo.getEndLongitude());
                        int resultTwo = orderdetailPojoMapper.insert(insertDetail);

                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = new Date(System.currentTimeMillis());
                        String format = formatter.format(date);
                        //存放订单详情
                        RMap<Object, Object> orderinfoMap = redissonClient.getMap("orderinfo:" + orderId);
                        orderinfoMap.put("orderId",orderId);
                        orderinfoMap.put("passengerId",qo.getPassengerId());

                        orderinfoMap.put("startName",qo.getStartName());
                        orderinfoMap.put("endName",qo.getEndName());

                        orderinfoMap.put("orderMileage",qo.getOrderMileage());
                        orderinfoMap.put("orderCost",qo.getOrderCost());

                        orderinfoMap.put("orderStatus",101);

                        System.out.println("format===="+format);
                        orderinfoMap.put("orderStatustime",format);
                        System.err.println("标记打印时间");
                        System.err.println(format);

                        orderinfoMap.put("startLongitude",qo.getStartLongitude());
                        orderinfoMap.put("startLatitude",qo.getStartLatitude());

                        orderinfoMap.put("endLongitude",qo.getEndLongitude());
                        orderinfoMap.put("endLatitude",qo.getEndLatitude());


                        orderinfoMap.put("passengerTel",passengerPojo.getPassengerTel());
                        orderinfoMap.expire(600, TimeUnit.SECONDS);



                        //添加到未被抢单订单池
                        redissonClient.getBucket("pend:order:"+orderId).set(orderId,60000,TimeUnit.SECONDS);

//                            area:A1
                        //redis 位置桶
                        //area:A1:orders
                        RGeo<Object> areaGeo = redissonClient.getGeo( areaName + ":orders");
                        areaGeo.add(new GeoEntry(qo.getStartLongitude(),qo.getStartLatitude(),orderId));

                        System.out.println("生成订单成功");
                        List<TakeOrderQo> qoList = new ArrayList<>();
                        qoList.add(qo);
                        return new BaseDto(101,"生成订单成功",qoList);
                    }
                }else {
                    continue;
                }
            }
            System.out.println("用户起点未开通服务");
            return new BaseDto(201,"用户起点未开通服务",null);
        }else{
            System.out.println("用户终点未开通服务");
            return new BaseDto(301,"用户终点未开通服务",null);
        }
    }
}




